//获取应用实例
const app = getApp()
import FormValidation from '../../../../../utils/formValidation'
Page({

  /**
   * 页面的初始数据
   */
	data: {
		//快递列表
		ExpressList: [],
		FrmValidate: null, //验证组件
		OrderPrice: '0', //订单金额
		FeeList: [], //费用列表

		HasDftAddress: false, //是否存在默认地址
		MailUserName: '', //收件名称
		BeforeMobile: '', //收件电话
		AfterMobile: '', //收件电话
		MailAreaAddress: '', //收件省市
		MailAddress: '', //收件地址
		MailAddressID: 0, //收件主键

		ExpressCompanyID: 0, //快递公司
		ExpressFee: 0, //快递费用

		BizTypeID: 0, //业务类型


		isShowBottomPop: false, //是否显示价格明细弹层
		isShowMask: false, //是否显示遮罩层
	},

  /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		this.setData(options);
		//获取订单费用
		this.getOrderPrice();
		// //获取邮寄公司
		// this.getExpressFee();
    // //获取默认收件地址
    // this.getDefaultAddress();
		//设置验证
		this.initValidate();
	},

  /**
   *  表单提交
   */
	formSubmit: function (e) {
		let self = this;
		const data = e.detail.value
		// 传入表单数据，调用验证方法
		if (!this.FrmValidate.checkForm(data)) {
			const error = this.FrmValidate.errorList[0];
			wx.showToast({
				title: error.msg,
				icon: 'none'
			})
			return false
		}
		//车牌号码
		data.PlateNo = self.data.provinceShort + data.PlateNo;

		self.submitOrder(data);
	},
  /**
    *  快递方式的选择
    */
	expressTap: function (e) {
		let self = this;
		let expid = e.currentTarget.dataset.expid;
		let fee = e.currentTarget.dataset.fee;
		if (expid == self.data.ExpressCompanyID) {
			return;
		}

		self.setData({
			ExpressCompanyID: expid
		});

		//更新费用
		let prices = [{
			FeeName: '快递费',
			Fee: fee
		}];
		self.evalOrderPrice(prices);
	},
  /**
   *  显示底部价格明细
   */
	toggleBottomPopTap: function () {
		let self = this;
		self.setData({
			isShowMask: !self.data.isShowMask,
			isShowBottomPop: !self.data.isShowBottomPop
		})
	},
  /**
    * 选择地址
    */
	goToAddressListTap: function (e) {
		let addrid = e.currentTarget.dataset.addrid;
		if (parseInt(addrid) > 0) {
			wx.navigateTo({
				url: '/pages/my/myAddress/myAddressList/myAddressList?currentAddressId=' + addrid + '&SourceUrl=Order',
			})
		} else {
			wx.navigateTo({
				url: '/pages/my/myAddress/editAddress/editAddress?SourceUrl=Order',
			})
		}
	},
  /**
  * 验证初始化
  */
	initValidate() {
		// 验证字段的规则
		const rules = {
			UserName: {
				required: true
			},
			IDNum: {
				required: true,
				idcard: true
			},
			BeforeMobile: {
				required: true,
				tel: true
			},
			AfterMobile: {
				required: true,
				tel: true
			},
			// MailAddressID: {
			// 	required: true,
			// 	min: 1
			// },
      // ExpressCompanyID: {
			// 	required: true,
			// 	min: 1
			// }
		};

		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			UserName: {
				required: '请输入申请人姓名'
			},
			IDNum: {
				required: '请输入申请人身份证号码',
				idcard: '请输入正确身份证号码'
			},
			BeforeMobile: {
				required: '请输入变更前电话号码',
				tel: '请输入正确的变更前电话号码'
			},
			AfterMobile: {
				required: '请输入变更后电话号码',
				tel: '请输入正确的变更后电话号码'
			},
			// MailAddressID: {
			// 	required: '请选择收件地址',
			// 	min: '请选择收件地址'
			// },
			// ExpressCompanyID: {
			// 	required: '请选择快递公司',
			// 	min: '请选择快递公司'
			// }
		};
		// 创建实例对象
		this.FrmValidate = new FormValidation(rules, messages)
	},
  /**
   * 获取订单金额
   */
	getOrderPrice() {
		let self = this;
		let url = app.apiBaseUrl + "/Common/GetOrderPrice";
		app.httpRequest({
			method: "GET",
			url: url,
			data: {
				cityId: app.locationInfo.cityId,
				bizTypeId: self.data.BizTypeID
			},
			success: function (res) {
				let rdata = res.data;
				if (rdata.status == 0) {
					self.evalOrderPrice(rdata.data);
				} else {
					wx.showToast({
						title: rdata.message,
						icon: 'none'
					});
				}
			}
		});
	},
  /**
   * 获取默认邮寄地址
   */
	getDefaultAddress() {
		let self = this;
		let url = app.apiBaseUrl + "/MailAddress/GetDefault";
		app.httpRequest({
			method: "GET",
			url: url,
			data: {
				userId: app.userInfo.UserID
			},
			success: function (res) {
				let rdata = res.data;
				if (rdata.status == 0) {
					if (rdata.data) {
						rdata.data.HasDftAddress = true;
						self.setData(rdata.data);
					}
				} else {
					wx.showToast({
						title: rdata.message,
						icon: 'none'
					});
				}
			}
		});
	},
  /**
   * 获取快递公司
   */
	getExpressFee() {
		let self = this;
		let url = app.apiBaseUrl + "/Common/GetExpressFee";
		app.httpRequest({
			method: "GET",
			url: url,
			data: {
				cityId: app.locationInfo.cityId,
				bizTypeId: self.data.BizTypeID
			},
			success: function (res) {
				let rdata = res.data;
				if (rdata.status == 0) {
					self.setData({
						ExpressList: rdata.data
					});

					//更新快递费用和总费用
					if (rdata.data.length == 1) {
						self.setData({
							ExpressCompanyID: rdata.data[0].ExpressCompanyID
						});
						//更新费用
						let prices = [{
							FeeName: '快递费',
							Fee: rdata.data[0].ExpressFee
						}];
						self.evalOrderPrice(prices);
					}
				} else {
					wx.showToast({
						title: rdata.message,
						icon: 'none'
					});
				}
			}
		});
	},
  /**
   * 设置总价格
   */
	evalOrderPrice(addList) {
		let self = this;
		let tmps = self.data.FeeList;
		let orderPrice = 0;

		//价格合并
		for (let i = 0; i < addList.length; i++) {
			let isExit = false;
			for (let j = 0; j < tmps.length; j++) {
				if (addList[i].FeeName == tmps[j].FeeName) {
					tmps[j].Fee = addList[i].Fee;
					isExit = true;
					break;
				}
			}
			if (!isExit) {
				tmps.push({
					FeeName: addList[i].FeeName,
					Fee: addList[i].Fee
				});
			}
		}

		//计算总金额
		for (let j = 0; j < tmps.length; j++) {
			orderPrice += parseFloat(tmps[j].Fee);
		}

		self.setData({
			FeeList: tmps,
			OrderPrice: orderPrice
		});
	},
  /**
   * 车牌大写
   */
	plateNoInput(e) {
		let v = e.detail.value;
		this.setData({
			PlateNo: v.toUpperCase()
		});
	},
  /**
   * 提交订单
   */
	submitOrder(data) {
		let self = this;
		data.UserID = app.userInfo.UserID;
		data.CityID = app.locationInfo.cityId;
		data.BizTypeID = self.data.BizTypeID;
		data.OrderPrice = self.data.OrderPrice;
		data.ExpressFee = self.data.ExpressFee;

		let url = app.apiBaseUrl + "/RegOrder/AddOrder";
		app.httpRequest({
			method: "POST",
			url: url,
			data: data,
			success: function (res) {
				let rdata = res.data;
				if (rdata.status == 0) {
					wx.navigateTo({
						url: '/pages/index/mainOperation/order/success/success',
					})
				} else {
					wx.showToast({
						title: rdata.message,
						icon: 'none'
					});
				}
			}
		});
	},


  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {

	}
})